/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.reportes;

import DBMS.DataBaseManager;
import clases.Reporte;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Gimbet
 */
public class GenerarReporte extends org.apache.struts.action.Action {

    /*
     * forward name="success" path=""
     */
    private static final String SUCCESS = "success";

    private static final String name = "Generar Reporte";
    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    
    public ActionForward pdf(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Reporte reporte = (Reporte) form;
        DataBaseManager dbms = DataBaseManager.getInstance();
        reporte = dbms.consultarReporte(reporte);
        //Obtenemos el reporte
        reporte.setReporte(dbms.consultarElementosReporte(reporte));
        String path = "C:/Users/Gimbet/Downloads/"+reporte.getNombre()+".csv";
        reporte.imprimirArchivoTabla(new FileOutputStream(path));
            request.getSession().setAttribute("path", path);
            request.setAttribute("path", path);
            return mapping.findForward("csv");

    }
    
    public ActionForward csv(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
                    DataBaseManager d = DataBaseManager.getInstance();
        Reporte reporte = (Reporte) form;
        System.out.println("Reporte ID"+ reporte.getIdreporte());
        reporte = d.consultarReporte(reporte);
        //Obtenemos el reporte
        reporte.setReporte(d.consultarElementosReporte(reporte));
        String path = "C:/Users/Gimbet/Downloads/"+reporte.getNombre()+".pdf";
        System.out.println("Intentando crear reporte en " + path);
        FileOutputStream f = new FileOutputStream(path);
        reporte.imprimirArchivoDoc(f);
        request.getSession().setAttribute("path", path);
        request.setAttribute("path", path);
        return mapping.findForward("pdf");
    }
}
